<body class="app" data-ad="true">
<?php $CDN= $this->CommonHelper()->imageDomain();?>
<section class="app-content">
	<section class="page page-index noline" data-plugin-type="info_front">

        <div class="top-bar">支付订单<a href="<?php echo $this->url('myorder', array('action'=>'index'));?>" class="btn-t-back"></a></div>
        <div class="time-counter bw">
            <p>支付剩余时间</p>
            <div class="m-time">60:00</div>
        </div>

        <div class="pay-item bw mt20">
            <dl>
                <dt><img src="<?php echo $CDN.$this->order['firstItem']['oi_image'];?>"></dt>
                <dd>
                    <p>¥<?php echo $this->order['order_total_price'];?></p>
                    <p class="p-price"><?php echo $this->order['firstItem']['oi_name'];?>......</p>
                </dd>
            </dl>
        </div>

        <div class="pay-type bw mt20">
        	<?php if ($hasBalancePay > 0) {?>
            <label><input type="radio" name="p-type" value="balance" checked>余额支付<span>余额¥<?php echo $this->user['balance'];?></span></label>
            <?php }?>
            <label><input type="radio" name="p-type" value="wx">微信支付</label>
        </div>

        <a class="btn-w btn-yel" href="javascript:pay();">确认支付 ¥<?php echo $this->order['order_total_price'];?></a>
        
        <form id="balanceForm" action="/order/balancePay" method='post'>
            <input id="order_no" name="order_no" type="hidden" value="<?php echo $this->order['order_no'];?>">
        </form>
	</section>

    <!-- 底部menu-->
    <?php echo $this->partial('public/menu.phtml'); ?>
</section>

<script type="text/javascript">
$(function () {
    //星星效果
    $('.score-list li').each(function(){
        $(this).find('span').click(function(){
            var _idx = $(this).index();
            $(this).parent('li').find('span:lt('+ _idx +')').addClass('on');
            $(this).parent('li').find('span:gt('+ _idx +')').removeClass('on');
        })
    })


    var maxstrlen = 50;
    var wck = $('.rel-line').find('i');
    $("#text-upload").keyup(function () {
        len = maxstrlen;
        var str = $(this).val();
        myLen = getStrleng(str);
        if (myLen > len * 2) {
            $(this).value = str.substring(0, i + 1);
        }
        else {
            wck.html(Math.floor((len * 2 - myLen) / 2));
        }
    });
    function getStrleng(str) {
        myLen = 0;
        i = 0;
        for (; (i < str.length) && (myLen <= maxstrlen * 2); i++) {
            if (str.charCodeAt(i) > 0 && str.charCodeAt(i) < 128)
                myLen++;
            else
                myLen += 2;
        }
        return myLen;
    }


});

//支付订单
function pay(){
	$pay_channel = $('input[name="p-type"]:checked').val();
	console.log($('input[name="p-type"]:checked').val());
	if ($pay_channel == 'balance') {		
		balancePay();		
	} else if ($pay_channel == 'wx') {
		callpay();
	} else {
		alert('请选择支付方式');
	}
}

function balancePay(){
	var balance = <?php echo $this->user['balance'];?>;
	var total_price = <?php echo $this->order['order_total_price'];?>;
	if (total_price > balance) {
		alert("账户余额不足，请选择其他支付方式");
		return false;
	}
	$("#balanceForm").submit();
}
</script>

<script type="text/javascript">

//调用微信JS api 支付
function jsApiCall()
{
	WeixinJSBridge.invoke(
		'getBrandWCPayRequest',
		<?php echo $this->jsApiParameters; ?>,
		function(res){
			WeixinJSBridge.log(res.err_msg);
			if(res.err_msg == "get_brand_wcpay_request:ok" ) {
				 window.location.href="<?php echo $this->url('order', array('action'=>'paymentDone','id'=>$this->order['order_no']));?>"; 
			} else {
				
				alert('支付失败，请重新支付');
			}          
		}
	);
}

function callpay()
{
	<?php if($this->error_code == 'ORDERPAID') {?>
		alert('该订单已支付');
		return false;
	<?php }?>
	if (typeof WeixinJSBridge == "undefined"){
	    if( document.addEventListener ){
	        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
	    }else if (document.attachEvent){
	        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
	        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
	    }
	}else{
	    jsApiCall();
	}
}
</script>
